from symbide.src.parsing import parsers
from symbide import PATHS
import gtk, os
import webbrowser
from shutil import rmtree

#TODO: No need that Export dialog is a class, convert in function and maybe integrate in dialogs
#TODO: write more documentation for dialog_export.py
class ExportDialog:
    def __init__(self, worksheet):
        #main dialog-window
        exportDialog = gtk.FileChooserDialog(
                                title='Symbide - Export Worksheet: '+worksheet.caption,
                                action=gtk.FILE_CHOOSER_ACTION_SAVE,                                            
                                buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                gtk.STOCK_CONVERT, gtk.RESPONSE_OK))
        exportDialog.show()
        
        self.worksheet = worksheet
        
        self.styleBox = StyleBox() #a ComboBox to choose the stylesheet
        self.modeBox = ModeBox(self) #a ComboBox to choose between xml/xsl or html/css
        previewButton = PreviewButton(self)
        
        l1 = gtk.Label('Export as:')
        l1.show()
        l2 = gtk.Label('Stylesheet:')
        l2.show()
        
        table = gtk.Table(rows=3, columns=2, homogeneous=False)
        table.attach(l1,0,1,0,1)
        table.attach(self.modeBox.widget,1,2,0,1)
        table.attach(l2,0,1,1,2)
        table.attach(self.styleBox.widget,1,2,1,2)
        table.attach(previewButton,1,2,2,3, ypadding=8)
        table.show()
    
        exportDialog.vbox.pack_end(table, expand=False)
        
        response = exportDialog.run()
        
        if response==gtk.RESPONSE_OK:
            stylePath = self.styleBox.getPath()
            path = exportDialog.get_filename()
            parsers['export'].export(worksheet.todom().documentElement, stylePath, path)
        
        exportDialog.destroy()

class PreviewButton(gtk.Button):
    def __init__(self, dialog):
        gtk.Button.__init__(self, 'Preview')
        self.show()
        self.connect('clicked', self._event_preview)
        self.dialog = dialog
        self.styleBox = dialog.styleBox
        
    def _event_preview(self, *args):
        mode = self.styleBox.mode
        path = os.path.join(PATHS['temp_html'],'index.%s'%mode.split('+')[0])
        
        p = os.path.join(PATHS['temp_html'],'plots')
        if os.path.exists(p):
            rmtree(p)
        
        p = os.path.join(PATHS['temp_html'],'stylesheets')
        if os.path.exists(p):
            rmtree(p)
        
        stylePath = self.styleBox.getPath()
        parsers['export'].export(self.dialog.worksheet.todom().documentElement, stylePath, path)
        webbrowser.open_new_tab(path)

class StyleBox:
    def __init__(self):
        self.widget = gtk.combo_box_new_text()
        self.widget.show()
        self.counter = 0
    
    def clear(self):
        for i in range(self.counter):
            self.widget.remove_text(0)
        self.counter = 0
        
    def setMode(self, mode):
        self.mode = mode
        self.dir = PATHS[mode.split('+')[1]]
        paths = os.listdir(self.dir)
        self.clear()
        
        for p in paths:
            longp = os.path.join(self.dir,p)
            if os.path.isfile(longp) and os.path.splitext(p)[1]=='.%s'%mode.split('+')[1]:
                self.counter+=1
                self.widget.append_text(p)
        self.widget.set_active(0)
        
    def getPath(self):
        return os.path.join(self.dir,self.widget.get_active_text()) 
  
      
class ModeBox:
    modes = ('xml+xsl', 'html+css')
    
    def __init__(self, dialog):
        self.styleBox = dialog.styleBox
        
        self.widget = gtk.combo_box_new_text()
        self.widget.show()
        
        for m in self.modes:
            self.widget.append_text(m)    
        self.widget.set_active(0)
        self.styleBox.setMode(self.modes[0])
        self.widget.connect('changed', self._event_changed)
        
    def _event_changed(self, *args):
        mode = self.widget.get_active_text()
        self.styleBox.setMode(mode)
        
    def getText(self):
        return self.widget.get_active_text()
